package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("CrawlerpictureDAO")
@Transactional
public class CrawlerpictureDAOImpl extends AbstractJpaDao<Crawlerpicture> implements CrawlerpictureDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawlerpicture.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public CrawlerpictureDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Crawlerpicture findCrawlerpictureById(Integer id) throws DataAccessException {

		return findCrawlerpictureById(id, -1, -1);
	}

	@Transactional
	public Crawlerpicture findCrawlerpictureById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerpictureById", startResult, maxRows, id);
			return (Crawlerpicture) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByName(String name) throws DataAccessException {

		return findCrawlerpictureByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerpictureByName", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerpicture>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByNameContaining(String name) throws DataAccessException {

		return findCrawlerpictureByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerpictureByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerpicture>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByPath(String path) throws DataAccessException {

		return findCrawlerpictureByPath(path, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByPath(String path, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerpictureByPath", startResult, maxRows, path);
		return new LinkedHashSet<Crawlerpicture>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByPathContaining(String path) throws DataAccessException {

		return findCrawlerpictureByPathContaining(path, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByPathContaining(String path, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerpictureByPathContaining", startResult, maxRows, path);
		return new LinkedHashSet<Crawlerpicture>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByDescription(String description) throws DataAccessException {

		return findCrawlerpictureByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerpictureByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerpicture>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByDescriptionContaining(String description)
			throws DataAccessException {

		return findCrawlerpictureByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerpictureByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerpicture>(query.getResultList());
	}

	@Transactional
	public Crawlerpicture findCrawlerpictureByPrimaryKey(Integer id) throws DataAccessException {
		return findCrawlerpictureByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Crawlerpicture findCrawlerpictureByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerpictureByPrimaryKey", startResult, maxRows, id);
			return (Crawlerpicture) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerpicture> findCrawlerpicturesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerpicturesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerpicture> findAllCrawlerpictures() throws DataAccessException {

		return findAllCrawlerpictures(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerpicture> findAllCrawlerpictures(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerpictures", startResult, maxRows);
		return new LinkedHashSet<Crawlerpicture>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Crawlerpicture> findCrawlerpicturesByCrawler(Integer crawlerId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerpicturesByCrawler", start, max, crawlerId);
		return new LinkedHashSet<Crawlerpicture>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerpicture> findCrawlerpictureByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Crawlerpicture>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Crawlerpicture> findCrawlerpicturesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerpicture.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Crawlerpicture> crawlerpictures = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return crawlerpictures;
	}

	@Transactional
	public Crawlerpicture findCrawlerpictureByPhysicalPrimaryKey(String path, Integer crawlerId)
			throws DataAccessException {
		return findCrawlerpictureByPhysicalPrimaryKey(path, crawlerId, -1, -1);
	}

	@Transactional
	public Crawlerpicture findCrawlerpictureByPhysicalPrimaryKey(String path, Integer crawlerId, int startResult,
			int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerpictureByPhyscialPrimaryKey", startResult, maxRows, path,
					crawlerId);
			return (Crawlerpicture) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Crawlerpicture entity) {
		return true;
	}
}
